//
//  TuijianViewController.m
//  New
//
//  Created by 才诗琦 on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//
#define W self.view.frame.size.width-110

#define N self.view.frame.size.width-20
#import "TuijianViewController.h"
#import "SliderView-Prefix.pch"


@interface TuijianViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation TuijianViewController
-(void)dealloc
{
    [super dealloc];
    [_dataArr release];
    [_lunboArr release];
    
    
    
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
        
        self.view.backgroundColor = [UIColor whiteColor];
        //tableView
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 160) style:UITableViewStylePlain];
       self.tableView.delegate = self;
        self.tableView.dataSource = self;
      //轮播图的位置
         _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width  , 190)];
        //注册重用池
        //cell 大图片cell
        [self.tableView registerClass:[TuijianCell class] forCellReuseIdentifier:@"cell"];
        //cell2 小图片cell
        [self.tableView registerClass:[TuijianCell2 class] forCellReuseIdentifier:@"cell2"];
       //下拉刷新
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dadaHandler)];
        [self.tableView.mj_header beginRefreshing];
        [self.tableView.mj_header endRefreshing];
        //上拉加载
        
//        self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(lowData)];
//        if (_dataArr.count != 0) {
//            [self.tableView.mj_footer beginRefreshing];
//        }
        
        
        
        
        //创建轮播图
        [self lunboTu];
        
        
        
        //添加父视图
        [self.view addSubview: self.tableView];
        
       //管理内存
        [_tableView release];
    }
    return self;
    
}
//下拉刷新
- (void)dadaHandler
{
    
    [AFNModel GETWithHandler:@"http://c.3g.163.com/nc/article/list/T1429173683626/0-20.html" completion:^(id result) {
        self.dataArr = [NSMutableArray array];
        self.dataArr = [TuijianModel modelHandlerWithArray:result[@"T1429173683626"]];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [self.tableView reloadData];
            
            // 拿到当前的下拉刷新控件，结束刷新状态
            [self.tableView.mj_header endRefreshing];
        });
        
    
    
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    
    
    }


#pragma mark -创建轮播图
- (void)lunboTu
{
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=SYDT10,SYRECOMMEND&newShowType=2&gv=4.6.9&av=0&proid=ifengnews&os=ios_8.4.1&vt=5&screen=750x1334&publishid=4002&uid=8e6e52d1d80716f49a4d94ed0c4c60e43a8c37da";
    
    [AFNModel GETWithHandler:str completion:^(id result) {
       //轮播图调用
        self.lunboArr = result[0][@"item"];
        NSMutableArray *photoArr = [NSMutableArray array];
        for (NSDictionary *dic in _lunboArr) {
            [photoArr addObject:dic[@"thumbnail"]];
        }
        //调用轮播图
        CarouselPicture *qqq = [[CarouselPicture alloc] initWithFrame:CGRectMake(5, 0, self.view.frame.size.width-20  , 170) timeInterval:2 imageUrlArr:photoArr urlArr:nil];
        [self.tableView addSubview:qqq];
        
        //管理内存
        [qqq release];
    }];
    
}


//方法实现
- (UITableView *)setWithUrlStr:(NSString *)str
{
  [AFNModel GETWithHandler:str completion:^(id result) {
        
        self.dataArr = [TuijianModel modelHandlerWithArray:result[@"T1429173683626"]];

        [self.tableView reloadData];
    }];
    
    return self.tableView;
}

///tableView的协议实现方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return  _dataArr.count;
}

//cell设置

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{TuijianModel *t = _dataArr[indexPath.row];
    
    //设置第一条数据位大图的cell
 if (indexPath.row == 0) {
    TuijianCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   [cell.imag sd_setImageWithURL:[NSURL URLWithString:t.imgsrc]];
    
    cell.titleLabel.text = t.title;
    cell.contentLabel.text = t.digest;
    cell.commentCountLabel.text = [[t.replyCount stringValue] stringByAppendingString:@"跟帖"];
    
        return cell;
}
//    其余的为小图的cell
    else
    {
        TuijianCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
      [cell2.imag sd_setImageWithURL:[NSURL URLWithString:t.imgsrc]];
        cell2.titleLabel.text = t.title;
        cell2.contentLabel.text = t.digest;
        cell2.commentCountLabel.text = [[t.replyCount stringValue] stringByAppendingString:@"跟帖"];
        return cell2;
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TuijianModel *t = _dataArr[indexPath.row];
    
    if (indexPath.row == 0) {
   CGFloat h = [TuijianCell heightWithText:t.digest width:N];
    
    return h +220;}
    
    else{
       
        if (t.digest.length == 0) {
            return 120;
        }
        else {
            CGFloat h = [TuijianCell2 heightWithText:t.digest width:W];
            return h + 80;}
    }
    
}

///点击跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TuijianModel *m = _dataArr[indexPath.row];
    [self.delegate pass:m.url];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
